package com.wolfword.crawler.server.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wolfword.common.domain.Result;
import com.wolfword.common.domain.ResultConstant;
import com.wolfword.common.domain.SessionData;
import com.wolfword.common.util.SessionUtil;
import com.wolfword.crawler.server.entity.UserInfoEntity;
import com.wolfword.crawler.server.service.LoginService;

@RestController
@RequestMapping("login")
@Api(value = "/login", description = "登录")
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	
	@ApiOperation("登录验证")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "loginName", value = "登录名", paramType = "query", dataType = "string", required = true),
			@ApiImplicitParam(name = "password", value = "密码", paramType = "query", dataType = "string", required = true) 
			})
	@PostMapping(value = "vertify")
	public Result list(HttpServletRequest request, String loginName,
			String password) {
		UserInfoEntity verify = loginService.verify(loginName, password);
		if (verify == null) {
			return new Result(ResultConstant.EMPTY_PASSWORD.setMessage("用户名或密码错误"));
		}
		SessionUtil.setSession(request, SessionData.userNameKey, verify.getUserName());
		SessionUtil.setSession(request, SessionData.loginNameKey, loginName);
		return new Result(ResultConstant.SUCCESS);
	}

}
